/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 版本: 苏州公安统一用户管理平台1.0
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名:EncryptUtil.java
 * <p>
 * 类产生时间: 2012-7-4 上午10:51:38
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 苏州公安统一用户管理平台
 * <p>
 * 所在部门: 电信/国土——技术二部
 * <p>
 * <p>
 */
package com.oceansoft.core.common.util;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.log4j.Logger;

/**
 * 加密工具类
 * 
 * @author chenw
 * 
 */
public class EncryptUtil {

	Logger log = Logger.getLogger(EncryptUtil.class);

	/**
	 * 加密盐值
	 */
	private static final String salt = "zhuweijiatanshuqi6612515zhuweijiatanshuqi6612515thinkpad";

	public static String encrypt(String content) throws Exception {
		return encrypt(content, salt);
	}

	public static String decrypt(String content) throws Exception {
		return decrypt(content, salt);
	}

	/**
	 * 字符串加密后比对认证
	 * 
	 * @param verifyStr
	 *            待检验字符串
	 * @param target
	 *            匹配字符串
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 * @throws Exception
	 */
	public static boolean verify(String verifyStr, String target) throws NoSuchAlgorithmException,
			UnsupportedEncodingException, Exception {
		return encrypt(MD5Util.encoderByMd5((verifyStr))).equals(target);
	}

	/**
	 * 加密
	 * 
	 * @param content
	 * @param password
	 * @return
	 * @throws Exception
	 */
	private static String encrypt(String content, String password) throws Exception {
		String res = "";
		if ("".equals(password.trim())) {
			password = salt;
		}
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			kgen.init(128, new SecureRandom(password.getBytes()));
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");
			byte[] byteContent = content.getBytes("UTF-8");
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[] result = cipher.doFinal(byteContent);
			for (byte b : result) {
				res += b + " ";
			}
			return res;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
	}

	/**
	 * 解密
	 * 
	 * @param content
	 * @param password
	 * @return
	 * @throws Exception
	 */
	private static String decrypt(String content, String password) throws Exception {
		String res = "";
		try {
			if ("".equals(password.trim())) {
				password = salt;
			}
			String[] s = content.split(" ");
			byte[] result = new byte[s.length];
			int i = 0;
			for (String str : s) {
				result[i] = Byte.parseByte(str);
				i++;
			}
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			kgen.init(128, new SecureRandom(password.getBytes()));
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.DECRYPT_MODE, key);
			result = cipher.doFinal(result);
			res = new String(result, "UTF-8");
			return res;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
	}

}
